package com.lukk.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;

import com.lukk.constant.IndentStatus;
import com.lukk.dao.IndentDAO;
import com.lukk.dao.MerchantDAO;
import com.lukk.pojo.Indent;
import com.lukk.service.IIndentService;

/**
 * 
 * IndentServiceImpl 上午10:16:03
 * 
 * Copyright zhaocj Inc. All rights reserved. Love ME Like Justin Bieber.
 */
@Service("indentService")
public class IndentServiceImpl implements IIndentService {
	private static Logger logger = LogManager.getLogger(IndentServiceImpl.class.getName());
	@Resource
	private IndentDAO indentDAO;
	@Resource
	private MerchantDAO merchantDAO;

	@Override
	public Map<String, Object> selectByIWorker(Integer offset, Integer limit, String sort, String order,
			String beginTime, String endTime, Integer type, String areaCode, Integer iWorkerID) {
		Map<String, Object> queryMap = new HashMap<String, Object>();
		Map<String, Object> retsMap = new HashMap<String, Object>();
		// 如果是按照区域划分
		if (type == 0) {
			if (StringUtils.trimToNull(areaCode) != null) {
				String[] split = StringUtils.trim(areaCode).split(",");
				queryMap.put("offset", offset);
				queryMap.put("limit", limit);
				queryMap.put("sort", sort);
				queryMap.put("order", order);
				queryMap.put("areaCode", split);
				queryMap.put("beginTime", beginTime);
				queryMap.put("endTime", endTime);
				List<Indent> beans = indentDAO.selectByIWorkerAndArea(queryMap);
				for (Indent indent : beans) {
					indent.setIndentStatus(IndentStatus.getType(indent.getStatus() + 1));
				}
				retsMap.put("total", indentDAO.selectCountByIWorkerAndArea(queryMap));
				retsMap.put("rows", beans);
				return retsMap;
			} else {
				retsMap.put("total", 0);
				retsMap.put("rows", new ArrayList<>());
				return retsMap;
			}

		}
		// 如果是按照划分商人划分
		else {
			queryMap.put("offset", offset);
			queryMap.put("limit", limit);
			queryMap.put("sort", sort);
			queryMap.put("order", order);
			queryMap.put("queryStatus", 1);
			queryMap.put("areaCode", null);
			queryMap.put("internalWorkerID", iWorkerID);
			List<Indent> beans = indentDAO.selectByIWorkerAndMerchant(queryMap);
			for (Indent indent : beans) {
				indent.setIndentStatus(IndentStatus.getType(indent.getStatus() + 1));
			}
			retsMap.put("total", indentDAO.selectCountByIWorkerAndMerchant(queryMap));
			retsMap.put("rows", beans);
			return retsMap;
		}
	}
}
